<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">
        您需要先添加收货地址才可提交订单。
      </div>
      <ul v-if="showAddress">
        <li>
          <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
        </li>
        <li>
          <span>联系方式：</span>
          {{ showAddress.contact.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2") }}
        </li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation }}</li>
      </ul>
      <a  @click="openAddressEdit(showAddress)" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <XtxButton @click="openDialog" class="btn">切换地址</XtxButton>
      <XtxButton class="btn" @click="openAddressEdit({})" >添加地址</XtxButton>
    </div>
  </div>

  <!-- 选择地址组件，根据list数据渲染 -->
  <XtxDialog title="切换收获地址" v-model:visible="visibleDialog">
    <div @click="selectedAddress=item" :class="{active:selectedAddress && selectedAddress.id === item.id}" class="text item" v-for="item in list" :key="item.id">
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li>
          <span>收货地址：</span >
          {{ item.fullLocation.replace(/ /g, "") + item.address }}
        </li>
      </ul>
    </div>

    <!-- 弹层底部按钮 -->
    <template #footer>
      <XtxButton
        type="gray"
        @click="visibleDialog = false"
        style="margin-right: 20px"
        >取消</XtxButton
      >
      <XtxButton @click="confirmAddressFn" type="primary" >确认</XtxButton>
    </template>
  </XtxDialog>

  <!-- 添加/编辑组件 -->
  <addressEdit @on-success="successHandler" ref="addressEdit"></addressEdit>
</template>
<script>
import { ref } from 'vue'
import addressEdit from './address-edit.vue'
export default {
  name: 'CheckoutAddress',
  props: {
    list: Array,
    default: () => []
  },

  components: { addressEdit },

  // 1. 在拥有根元素的组件中，触发自定义事件使用，有没有emit组件都一样
  // 2. 如果组件渲染的代码片段，Vue3.0中规定，需要在emits中申明你所触发的自定义事件
  // 3. 提倡：所触发的自定义事件，建议在emits中定义
  emits: ['change'],
  setup (props, { emit }) {
    const showAddress = ref(null) // 显示哪个收获地址

    const visibleDialog = ref(false) // 地址选择框显示隐藏

    const selectedAddress = ref(null) // 记录当前选中的地址的id

    // 查询是否有默认收获地址
    const defaultAddress = props.list.find((item) => item.isDefault === 0)

    // 如果有默认收获地址，使用默认收获地址
    if (defaultAddress) {
      showAddress.value = defaultAddress
    } else {
      // eslint-disable-next-line vue/no-setup-props-destructure
      if (props.list.length) showAddress.value = props.list[0]
    }

    // 将地址ID传递给父组件，父组件下单时需要使用地址
    emit('change', showAddress.value && showAddress.value.id)

    // 点击确定，将选择的地址中的ID传递给父组件
    const confirmAddressFn = () => {
      visibleDialog.value = false // 关闭弹窗
      showAddress.value = selectedAddress.value // 将选择地址的ID，传递，进行显示
      emit('change', selectedAddress.value.id) // 将地址的ID传递给父组件，用于下单
    }

    // 控制切换地址组件的显示与隐藏
    const openDialog = () => {
      // 将之前的选择的地址置空，否则会导致选择地址后，下一次选择，被选的依旧被选中
      selectedAddress.value = null
      visibleDialog.value = true // 通知弹层组件，可以打开了
    }

    // 控制编辑与添加组件 打开/关闭
    const addressEdit = ref(null)
    const openAddressEdit = (address) => {
      // address 可以用来判断是新增还是修改
      addressEdit.value.open(address)
    }

    // 添加新的收获地址 向list中追加数据
    const successHandler = (formData) => {
      // 根据formData中的Id来判断是修改还是添加  添加的ID是新的ID，修改的Id在当前的地址列表中来查询
      const address = props.list.find(item => item.id === formData.id)
      if (address) {
        for (const key in address) {
          address[key] = formData[key]
        }
      } else {
        const josnStr = JSON.stringify(formData)
        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(JSON.parse(josnStr))
      }
    }

    return { showAddress, visibleDialog, selectedAddress, confirmAddressFn, openDialog, addressEdit, openAddressEdit, successHandler }
  }
}
</script>
<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}

.xtx-dialog {
  overflow: auto;
  .text {
    flex: 1;
    max-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 20px;
      }
    }
  }
}
</style>
